package ch.unizh.ifi.ailab.gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.apache.log4j.Logger;

import ch.unizh.ifi.ailab.config.LocalizationSettings;
import ch.unizh.ifi.ailab.config.ProgramSettings;

/**
 * @author hans jaeckle jaeckle@gmail.com
 * 
 */
public class EditorTab extends JPanel {

	// die properties
	private static ProgramSettings properties = ProgramSettings
			.getProgramSettings();

	// localization
	private static LocalizationSettings localization = LocalizationSettings
			.getLocalizationSettings();

	// logging
	private Logger myLog = Logger.getLogger(Gui2007.class); // @jve:decl-index=0:

	private static final long serialVersionUID = 1L;

	private JPanel buttonPanel = null;

	private JButton saveButton = null;

	private JButton loadButton = null;

	private JButton loadDefaultsButton = null;

	private JLabel placeholder1 = null;

	private JLabel placeholder2 = null;

	private JPanel editingPanel = null;

	private JScrollPane editingScrollPane = null;

	private JTable editingTable = null;

	private JPanel infoPanel = null;

	private JLabel infoLine1 = null;

	private JLabel infoLine2 = null;

	/**
	 * This is the default constructor
	 */
	public EditorTab() {
		super();

		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new BorderLayout());
		this.add(getButtonPanel(), BorderLayout.SOUTH);
		this.add(getEditingPanel(), BorderLayout.CENTER);
		this.add(getInfoPanel(), BorderLayout.NORTH);
	}

	private void saveProperties() {
		myLog.debug("edit: save properties.");
	}

	private void loadProperties() {
		myLog.debug("edit: load properties.");
		fillEditingTable();
	}

	private void loadDefaultProperties() {
		myLog.debug("edit: load default properties.");
	}

	private void fillEditingTable() {
		// getEditingTable().add
		String columns[] = {
				localization.getProperty("loc-EditorTab-cloumnsKey"),
				localization.getProperty("loc-EditorTab-cloumnsValue") };

		Set set = properties.keySet();
		String data[][] = new String[properties.keySet().size()][2];

		Iterator i = set.iterator();
		int j = 0;
		myLog.debug("fill table: " + set.size());
		while (i.hasNext()) {
			String key = (String) i.next().toString();
			data[j][0] = key;
			data[j][1] = properties.getProperty(key);
			j++;
		}

		myLog.debug("table: scrollpanesize = "
				+ getEditingScrollPane().getSize().width + ":"
				+ getEditingScrollPane().getSize().height);

		editingTable = new JTable(data, columns);
		editingTable.setShowVerticalLines(true);
		editingTable.setShowHorizontalLines(true);
		editingTable.setShowGrid(true);
		editingTable.setSize(getEditingScrollPane().getSize());
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 3;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 4;
			placeholder2 = new JLabel();
			placeholder2.setText("   ");
			placeholder1 = new JLabel();
			placeholder1.setText("   ");
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getSaveButton(), gridBagConstraints);
			buttonPanel.add(placeholder1, gridBagConstraints1);
			buttonPanel.add(getLoadDefaultsButton(), gridBagConstraints2);
			buttonPanel.add(placeholder2, gridBagConstraints3);
			buttonPanel.add(getLoadButton(), gridBagConstraints4);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes saveButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSaveButton() {
		if (saveButton == null) {
			saveButton = new JButton();
			saveButton.setText(localization.getProperty("loc-EditorTab-saveButton"));
			saveButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					saveProperties();
				}
			});
		}
		return saveButton;
	}

	/**
	 * This method initializes loadButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoadButton() {
		if (loadButton == null) {
			loadButton = new JButton();
			loadButton.setText(localization.getProperty("loc-EditorTab-loadButton"));
			loadButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					loadProperties();
				}
			});
		}
		return loadButton;
	}

	/**
	 * This method initializes loadDefaultsButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoadDefaultsButton() {
		if (loadDefaultsButton == null) {
			loadDefaultsButton = new JButton();
			loadDefaultsButton.setText("Load Defaults");
			loadDefaultsButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							loadDefaultProperties();
						}
					});
		}
		return loadDefaultsButton;
	}

	/**
	 * This method initializes editingPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getEditingPanel() {
		if (editingPanel == null) {
			editingPanel = new JPanel();
			editingPanel.setLayout(new BorderLayout());
			editingPanel.add(getEditingScrollPane(), BorderLayout.NORTH);
		}
		return editingPanel;
	}

	/**
	 * This method initializes editingScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getEditingScrollPane() {
		if (editingScrollPane == null) {
			editingScrollPane = new JScrollPane();
			editingScrollPane.setViewportView(getEditingTable());
			editingScrollPane.setAutoscrolls(true);
		}
		return editingScrollPane;
	}

	/**
	 * This method initializes editingTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getEditingTable() {
		if (editingTable == null) {
			fillEditingTable();
			// editingTable = new JTable();
		}
		return editingTable;
	}

	/**
	 * This method initializes infoPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getInfoPanel() {
		if (infoPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridy = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints6.gridy = 0;
			infoLine2 = new JLabel();
			infoLine2.setText(localization.getProperty("loc-EditorTab-infoLine2"));
			infoLine1 = new JLabel();
			infoLine1.setText(localization.getProperty("loc-EditorTab-infoLine1"));
			infoPanel = new JPanel();
			infoPanel.setLayout(new GridBagLayout());
			infoPanel.add(infoLine1, gridBagConstraints6);
			infoPanel.add(infoLine2, gridBagConstraints5);
		}
		return infoPanel;
	}
}
